mod ast;
mod lexer;
mod parser;
mod token;

use std::error::Error;

use clap::{Parser as ClapParser, ValueEnum};
use parser::Parser;

#[derive(ClapParser, Debug)]
#[command(version, long_about = None, about="toy compiler")]
struct Args {
    #[arg(
        value_enum,
        long,
        default_value_t = Action::Ast,
        help = "Select the kind of output desired",
        value_name = "actin"
    )]
    emit: Action,

    #[arg(index = 1, value_name = "input toy file", default_value_t = {"-".to_string()})]
    filename: String,
}

#[derive(ValueEnum, Clone, Debug)]
enum Action {
    #[clap(help = "output the AST dump")]
    Ast,
}

fn main() -> Result<(), Box<dyn Error>> {
    let cli = Args::parse();

    Parser::new(cli.filename)
        .and_then(|mut p| p.parse())
        .map(|m| {
            if m.functions.is_empty() {
                eprintln!("Parse error: empty module");
            } else {
                println!("{}", m)
            }
        })?;

    Ok(())
}
